package com.app.service.impl;

import com.app.common.SystemConstant;
import com.app.mapper.AdvertisementMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.StringTool;
import com.app.util.TokenUtils;
import com.app.vo.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:首页图集实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class AdvertisementServiceImpl implements AdvertisementService {
    @Autowired
    AdvertisementMapper advertisementMapper;

    @Override
    public List<AdvertisementModel> getListByType(int userType,int seat) {
        QueryWrapper<AdvertisementModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", userType);
        if (seat > 0) {
            queryWrapper.eq("seat", seat);
        }
        List<AdvertisementModel> userList = advertisementMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public Page<AdvertisementModel> getPageByModel(AdvertisementModel model) {
        QueryWrapper<AdvertisementModel> queryWrapper = new QueryWrapper<>();
        if (model.getUserType() != null && model.getUserType() > 0) {
            queryWrapper.eq("user_type", model.getUserType());
        }
        if (model.getSeat() != null && model.getSeat() > 0) {
            queryWrapper.eq("seat", model.getSeat());
        }
        queryWrapper.orderByDesc("crt_time");
        Page<AdvertisementModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<AdvertisementModel> modelPage = advertisementMapper.selectPage(page, queryWrapper);
        if (modelPage == null) {
            return null;
        } else {
            return modelPage;
        }
    }
    @Override
    public AdvertisementModel getModelById(Integer id) {
        QueryWrapper<AdvertisementModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        AdvertisementModel model = advertisementMapper.selectOne(queryWrapper);
       return model;
    }

    @Override
    public void update(AdvertisementModel model) {
        advertisementMapper.updateById(model);
    }

    @Override
    public int insert(AdvertisementModel model) {
        return advertisementMapper.insert(model);
    }

    @Override
    public void delete(Integer id) {
        advertisementMapper.deleteById(id);
    }
}
